/************************************************************************
*************************************************************************
*************************************************************************

prodestcon_calibrate2.do

Calibrates the parameters to GNR 2013 for Chile, Industry 311. This version
actually replicates their procedure and uses the outcome to estimate the
stochastic processes.

*************************************************************************
*************************************************************************
************************************************************************/

/************************************************************************
Replicate GNR for Industry  311
************************************************************************/
clear
cap program drop gmm_prod
do "do/progs_from_gnr/gmm_prod.ado"




tempfile estimates
postfile estimates ind mark1 mark2 mark3 etasd ar om0 om1 om2 om3 meanomega sdprod shksd  klag keta1 keta2 llag leta1 leta2 sar1 sar2 ssd nyr nfirm using `estimates'



foreach ind in 311 321 322 331 381 {


	do "do/progs_from_gnr/repgnr.do" `ind'



	/************************************************************************
	Estimate the Markov process and the innovation in productivity
	************************************************************************/

	//GNR assume the markov process is a degree 3 polynomial; let's take that and
	//run with it
	gen lagomega = l.logomega
	gen lagomega2 = lagomega^2
	gen lagomega3 = lagomega^3

	reg logomega lagomega*
	predict etapred, res

	local mark1 = _b[lagomega]
	local mark2 = _b[lagomega2]
	local mark3 = _b[lagomega3]
	
	sum etapred		
	local etasd = r(sd)

	/************************************************************************
	Estimate the AR process in productivity
	************************************************************************/
	reg logomega lagomega

	local ar	= _b[lagomega]
	
	
	/************************************************************************
	Estimate the cubic markov process in productivity
	************************************************************************/
	reg logomega lagomega c.lagomega#c.lagomega c.lagomega#c.lagomega#c.lagomega

	local om0	= _b[_cons]
	local om1	= _b[lagomega]
	local om2	= _b[c.lagomega#c.lagomega ]
	local om3	= _b[c.lagomega#c.lagomega#c.lagomega]

	/************************************************************************
	Estimate the standard deviation of productivity
	************************************************************************/
	sum logomega
	
	local meanomega = r(mean)
	local sdprod = r(sd)


	/************************************************************************
	Estimate the standard deviation of the shock
	************************************************************************/
	reg yg logomega c.(k l i)##c.(k l i)##c.(k l i)
	gen samp = e(sample)
	predict eps if samp, resid
	sum eps					if samp

	local shksd 	= r(sd)


	/************************************************************************
	Estimate the stochastic processes for capital and labor
	************************************************************************/

	//These depend on their own lags and on the innovations; note that they
	//are NOT Markov

	reg logK l.logK l.etapred l2.etapred
	local klag 	= _b[l.logK]
	local keta1	= _b[l.etapred]
	local keta2	= _b[l2.etapred]

	reg logL l.logL l.etapred l2.etapred
	local llag 	= _b[l.logL]
	local leta1	= _b[l.etapred]
	local leta2	= _b[l2.etapred]




	/************************************************************************
	Estimate the stochastic processes for savings
	************************************************************************/

	gen S = log(stassets) if samp
	sum S

	reg S l(1/2).S

	local sar1 = _b[l.S]
	local sar2 = _b[l2.S]

	predict Sres, resid
	sum Sres

	local ssd 	= r(sd)

	/************************************************************************
	Get the standard deviation and persistence of prices
	************************************************************************/

	gen logp = log(defcons_ye) if samp

	reg logp l.logp if samp
	predict logpres if samp, resid
	sum logpres



	/************************************************************************
	For Appendix: Estimate the Reduced-Form Choice Function
	************************************************************************/
	reg i c.(k l logomega)##c.(k l logomega) if samp



	/************************************************************************
	Average number of observations per firm
	************************************************************************/
	keep if samp
	bysort id: gen nobs = _N
	egen tag = tag(id)
	sum nobs if tag

	local nyr 	= r(mean)
	count if tag
	local nfirm = r(N)

	post estimates (`ind') (`mark1') (`mark2') (`mark3') (`etasd') (`ar') ///
					(`om0') (`om1') (`om2') (`om3') (`meanomega') /// 
					(`sdprod') (`shksd')  (`klag') (`keta1') (`keta2') (`llag') ///
					(`leta1') (`leta2') (`sar1') (`sar2') (`ssd') (`nyr') (`nfirm')
}

postclose estimates

use `estimates', clear



save "dta/emp_results/calibration_chile.dta", replace



